import { createI18n } from "vue-i18n";

const modules: any = import.meta.glob("./locales/*.ts", {
  eager: true,
  import: "default"
});
interface messagesType {
  [propname: string]: {
    [propname: string]: string;
  };
}
const messages: messagesType = {};
Object.keys(modules).forEach(key => {
  const filename = key.replace(/.*\/locales\/([^\/]*)\.ts/gi, "$1");
  messages[filename] = modules[key];
});
const langList = ["en", "th"];
const sysLang = navigator.language.split("-")[0];
const { getLocalLang, setLocalLang } = useLocal();
const userLang = getLocalLang() || (langList.includes(sysLang) ? sysLang : "en");
setLocalLang(userLang);

const i18n = createI18n({
  locale: userLang,
  globalInjection: true,
  legacy: false,
  messages: messages
});

export const { t, locale } = i18n.global;

export default i18n;

/*let timer = setTimeout(() => {
  clearTimeout(timer);
  // 3秒后切换语言
  LocalStorage.setItem("language", "ch");
  i18n.global.locale.value = "ch";
  window.location.reload();
}, 3000);
*/
